//package com.mashuai.utils.ztest.toolkit.git;
//
///**
// * @ClassName: TestCountByGit
// * @Author : mashuai
// * @Description: TODO
// */
//import org.eclipse.jgit.api.Git;
//import org.eclipse.jgit.api.LogCommand;
//import org.eclipse.jgit.api.errors.GitAPIException;
//import org.eclipse.jgit.diff.*;
//import org.eclipse.jgit.lib.ObjectId;
//import org.eclipse.jgit.lib.PersonIdent;
//import org.eclipse.jgit.patch.FileHeader;
//import org.eclipse.jgit.revwalk.RevCommit;
//import org.eclipse.jgit.revwalk.RevWalk;
//import org.eclipse.jgit.util.io.DisabledOutputStream;
//
//import java.io.IOException;
//import java.nio.file.Path;
//import java.nio.file.Paths;
//import java.time.LocalDate;
//import java.util.List;
//
//public class TestCountByGit {
//    public static void main(String[] args) {
//        // 1. 获取今天的日期
//        LocalDate today = LocalDate.now();
//
//        // 2. 打开项目文件夹
//        String projectFolder = "D:\\Projects\\GithubTest\\springboot_hb";
//        Path folderPath = Paths.get(projectFolder);
//
//        // 3. 获取昨天的日期
//        LocalDate yesterday = today.minusDays(1);
//
//        // 4. 统计行数
//        int totalLines = countLinesSinceYesterday(folderPath, yesterday);
//
//        // 5. 输出统计结果
//        String result = today.toString() + " 提交的代码行数为：" + totalLines;
//        System.out.println(result);
//    }
//
//    private static int countLinesSinceYesterday(Path folderPath, LocalDate yesterday) {
//        int totalLines = 0;
//
//        try (Git git = Git.open(folderPath.toFile())) {
//            // 使用JGit获取Git提交记录
//            LogCommand logCommand = git.log();
//            Iterable<RevCommit> commits = logCommand.call();
//
//            for (RevCommit commit : commits) {
//                // 获取提交的时间戳
//                PersonIdent author = commit.getAuthorIdent();
//                long commitTime = author.getWhen().getTime();
//
//                // 将时间戳转换为LocalDate对象
//                LocalDate commitDate = LocalDate.ofEpochDay(commitTime / (24 * 60 * 60 * 1000));
//
//                // 判断提交日期是否在昨天之后
//                if (commitDate.isAfter(yesterday)) {
//                    // 获取提交的代码更改
//                    try (RevWalk revWalk = new RevWalk(git.getRepository())) {
//                        RevCommit parentCommit = revWalk.parseCommit(commit.getParent(0));
//                        int linesChanged = getLinesChanged(git, parentCommit.getId(), commit.getId());
//                        totalLines += linesChanged;
//                    }
//                }
//            }
//        } catch (IOException | GitAPIException e) {
//            e.printStackTrace();
//        }
//
//        return totalLines;
//    }
//
//    private static int getLinesChanged(Git git, ObjectId oldCommit, ObjectId newCommit) throws IOException {
//        // 使用JGit获取两个提交之间的代码更改，并统计新增行数
//        DiffFormatter diffFormatter = new DiffFormatter(DisabledOutputStream.INSTANCE);
//        diffFormatter.setRepository(git.getRepository());
//        diffFormatter.setDiffComparator(RawTextComparator.DEFAULT);
//        diffFormatter.setDetectRenames(true);
//
//        List<DiffEntry> diffs = diffFormatter.scan(oldCommit, newCommit);
//        int linesChanged = 0;
//
//        for (DiffEntry diff : diffs) {
//            FileHeader fileHeader = diffFormatter.toFileHeader(diff);
//            EditList editList = fileHeader.toEditList();
//
//            for (Edit edit : editList) {
//                linesChanged += edit.getEndB() - edit.getBeginB();
//            }
//        }
//
//        return linesChanged;
//    }
//}